<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>9.居中对齐</title>
  <style>
    .container {
      border: 1px solid #ccc;
      padding: 10px;
      margin: 10px;
      height: 200px;
    }
    .item {
      background-color: #ccc;
    }
    .container.container-1 {
      text-align: center;
      line-height: 200px;
    }

    .container-2 {
      position: relative;
    }

    .container-2 .item {
      width: 300px;
      height: 100px;
      position: absolute;
      left: 50%;
      margin-left: -150px;
      top: 50%;
      margin-top: -50px;
    }

    .container-3 {
      position: relative;
    }

    .container-3 .item {
      width: 300px;
      height: 100px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }

    .container-4 {
      position: relative;
    }

    .container-4 .item {
      width: 300px;
      height: 100px;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      margin: auto;
    }
  </style>
</head>
<body>
  <!-- 9.居中对齐 -->

  <!-- 水平居中 -->
  1.inline元素 text-align: center 
  2.block元素 margin: 0 auto; 
  3.absolute元素 left: 50% + margin-left 负值

  <!-- 垂直居中 -->
  1.inline元素 line-height值等于height值
  2.absolute 元素: top:50% + margin-top负值
  3.absolute元素: transform: translate(-50%, -50%)
  4.absolute元素: top left bottom right = 0 + margin: auto

  <div class="container container-1">
    <span>一段文字</span>
  </div>

  <div class="container container-2">
    <div class="item">this is item2</div>
  </div>

  <div class="container container-3">
    <div class="item">this is  item3</div>
  </div>

  <div class="container container-4">
    <div class="item">this is  item4</div>
  </div>
  
</body>
</html>